/*
	Colorbox.draggable	v0.5
  A Colorbox extension to make colorbox draggable
	Tested in IE 6-8, Firefox 3.x, Opera 10, Safari 4 (windows), Chrome 1.x
  
	AUTHOR: Dan Regazzi, 1/21/2010
*/
$(document).ready(function(){
	$().bind('cbox_open',function(){
		$("div[id*='cbox']").not("[id='cboxWrapper']").not("[id='cboxContent']").not("[id='cboxOverlay']").mousedown(function(e){addDrag(this, e);});
		$("div[id*='cbox']").not("[id='cboxWrapper']").not("[id='cboxContent']").not("[id='cboxOverlay']").mouseup(function(){removeDrag(this);});		

	});
	
	function addDrag(element, e){
		$('#colorbox').addClass('draggable');
		this.cursorPos = {X: e.pageX, Y: e.pageY};
		
		$(document).mousemove(function(e){
			if($('#colorbox').hasClass('draggable')){
				var colorboxCoords = {X: $('#colorbox').offset().left, Y: $('#colorbox').offset().top};
				var pageCoords = {X: e.pageX, Y: e.pageY};
				
				$('#colorbox').css({'left': colorboxCoords.X - (cursorPos.X - pageCoords.X), 'top': colorboxCoords.Y - (cursorPos.Y - pageCoords.Y)});
				cursorPos = pageCoords;
			}
		});
	}
	
	function removeDrag(element){
		$('#colorbox').removeClass('draggable');
		$(element).unbind('mousemove');
	}
	
	$().bind('cbox_close',function(){
		$('#cboxWrapper').unbind('mousemove');
		$('#cboxTitle').unbind('mousemove');
	});
});